package org.hd.rolelist.service.impl;

import org.hd.rolelist.dao.RoleListDao;
import org.hd.rolelist.service.RoleListService;
import org.hd.sidemenu.dao.SideMenuDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleListServiceImpl implements RoleListService {

    @Resource
    RoleListDao roleListDao;


    /**
     * 获取角色列表
     *
     * @return
     */
    @Override
    public Map<String, Object> getRoleList() {

        Map<String,Object> re = new HashMap<>();
        //角色列表
        List<Map<String, String>> roleList = roleListDao.getRoleList();
        //所有的按钮
        List<String> menusAndParent = roleListDao.getMenusAndParent();

        //所有的按钮集合
//        List<Map<String,Object>> menus = new ArrayList<>();
//
//        for(int i = 0;i<menusAndParent.size();i++){
//            if(menusAndParent.get(i).get("parent") == null){
//                Map<String,Object> map = new HashMap<>();
//                map.put("description",menusAndParent.get(i).get("description"));
//                map.put("parent",menusAndParent.get(i).get("url"));
//                map.put("children",new ArrayList<Map<String,String>>());
//                menus.add(map);
//            }
//        }
//
//        for(int i = 0;i<menus.size();i++){
//            for(int j=0;j<menusAndParent.size();j++){
//                if(menus.get(i).get("parent").equals(menusAndParent.get(j).get("parent"))){
//                    ArrayList<Map<String,String>> arr = (ArrayList)menus.get(i).get("children");
//                    HashMap<String,String> map = new HashMap<>();
//                    map.put("description",menusAndParent.get(j).get("description"));
//                    map.put("url",menusAndParent.get(j).get("url"));
//                    arr.add(map);
//                }
//            }
//        }

        re.put("menus",menusAndParent);


        List<Map<String,Object>> result = new ArrayList<>();
        String roleName = "";
        if(roleList.size() != 0){
            for(int i = 0;i<roleList.size();i++){
                if(!roleName.equals(roleList.get(i).get("roleName"))){
                    roleName = roleList.get(i).get("roleName");
                    Map<String,Object> map = new HashMap<>();
                    map.put("roleName",roleName);
                    map.put("rstatus",roleList.get(i).get("rstatus"));
                    map.put("children",new ArrayList<Map<String,String>>());
                    result.add(map);
                }
            }
        }

        for(int i = 0;i<result.size();i++){
            ArrayList<Map<String,String>> arr = (ArrayList)result.get(i).get("children");
            for(int j = 0;j<roleList.size();j++){
                if(result.get(i).get("roleName").equals(roleList.get(j).get("roleName"))){
                    HashMap<String,String> map = new HashMap<>();
                    map.put("pstatus",roleList.get(j).get("pstatus"));
                    map.put("pName",roleList.get(j).get("pName"));
                    arr.add(map);
                }
            }
        }
        re.put("roleList",result);

        return re;
    }

    /***
     * 修改角色状态
     * @param roleName
     * @param status
     * @return
     */
    @Override
    public boolean changeRoleStatus(String roleName, boolean status) {
        return roleListDao.changeRoleStatus(roleName,status?1:0);
    }

    /***
     * 修改角色的某个权限
     * @param roleName
     * @param rightName
     * @return 1:成功 2:关系已存在 3：失败
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int changeRight(String roleName, String rightName,String oldRight) {
        Map<String, String> map = roleListDao.checkExitRight(roleName, rightName);
        if(map != null){
            return 2;
        }else{ //接下来查父按钮
            String parent = roleListDao.getParent(rightName);
            //查看父按钮是否有了
            Map<String, String> map1 = roleListDao.checkExitRight(roleName, parent);
            if(map1 == null){  //如果父按钮没有绑定
                //先插入父按钮
                boolean b = roleListDao.insertRoleRight(roleName, parent);
                if(b){
                    boolean c = roleListDao.updateRight(roleName, rightName,oldRight);
                    if(c){
                        return 1; //成功
                    }else{
                        return 3; //失败
                    }
                }
            }else{ //如果夫按钮绑定了
                if(roleListDao.updateRight(roleName, rightName,oldRight)){
                    return 1;
                }else{
                    return 3;
                }

            }
        }
        return 3;
    }

    /***
     * 删除某个角色下的权限
     * @param roleName
     * @param rightName
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteRight(String roleName, String rightName) {
        //先获取父按钮
        String parent = roleListDao.getParent(rightName);

        //判断是否要删除父按钮
        boolean b = roleListDao.deleteRight(roleName, rightName);


        if(b){
            List<String> allParent = roleListDao.getAllParent(roleName);
            if(allParent.size() == 0|| allParent==null){
                return true;
            }else{
                //对比是否还有其他子按钮存在
                for(int i =0 ;i<allParent.size();i++){
                    if(allParent.get(i) != null && allParent.get(i).equals(parent)){ //如果还有其他的子按钮，就当作成功
                        return true;
                    }
                }
                //对比完了，就得删除父按钮了
                if(roleListDao.deleteRight(roleName,parent)){
                    return true;
                }
            }
        }else{
            return false;
        }
        return false;
    }

    /**
     * 删除角色
     *
     * @param roleName
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteRole(String roleName) {

        if(roleListDao.deleteRolePer(roleName)){
            boolean b = roleListDao.deleteRole(roleName);
            if(b){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    /***
     * 插入新的角色
     * @param roleName
     * @param rolePer
     * @return 1:插入成功 2:角色名重复 3:插入失败
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNewRole(String roleName, ArrayList<String> rolePer) {

        if(roleListDao.checkExitRole(roleName) != null){
            return 2;
        }else {
            //先插入角色
            boolean insertRole = roleListDao.insertRole(roleName, String.valueOf(System.currentTimeMillis()));
            if (insertRole) {
                for (int i = 0; i < rolePer.size(); i++) {
                    //先查询父按扭是谁
                    String parentLabel = roleListDao.getParentLabel(rolePer.get(i));
                    //再插入关系
                    boolean b = roleListDao.insertRoleRight(roleName, rolePer.get(i));
                    if (b) {
                        //再查询是否存在父按钮
                        Map<String, String> exitRoleParentRight = roleListDao.getExitRoleParentRight(roleName, parentLabel);
                        if (exitRoleParentRight == null) {
                            roleListDao.insertRoleRight(roleName, parentLabel);
                        }
                    } else {
                        return 3;
                    }
                }
            }else{
                return 3;
            }
        }

        return 1;
    }


}
